package com.originskyseed.platform.common.api.util;

import java.util.Random;

public class PasswordUtil {

	/**
	 * 生成密码
	 *
	 * @param length
	 * @return
	 */
	public static String getStringRandom(int length) {
		String strPassword = "123456aB!";
		while (true) {
			String[] strings = { "@", "!", "#", "$", "%", "&", "*", ".", ",", "?" };
			// 保存在session中 当提交密码时再提交到数据库
			StringBuilder password = new StringBuilder("");
			Random random = new Random();
			password.append(random.nextInt(9));
			password.append(random.nextInt(9));
			for (int i = 0; i < 6; i++) {
				// 输出是大写字母还是小写字母
				int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
				password.append((char) (random.nextInt(26) + temp));
			}
			password.append(strings[random.nextInt(9)]);
			strPassword = password.toString();
			if (strPassword.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[~!@#$%^&*()_+`\\-={}:\";'<>?,.\\/]).{8,16}$")) {
				break;
			}
		}
		return strPassword;
	}

}
